<script>
    var url = '{{ .CommonIn.GetUrlByKey "admin/menu/doMenuSubmit" }}';
    function subData (data){
        var loadIndex = layer.load(2);
        $.ajax({
            type: "post",
            url: url,
            data:data,
            success: function (res) {
                layer.close(loadIndex);
                console.log(res)
                if(res.code==0){
                    layer.alert(res.msg,function(index){
                        parent.CloseChild(true)
                    })
                }else{
                    layer.alert(res.msg)
                }
            },
            error:function(){
                layer.close(loadIndex);
            },
            complete:function (){

            }
        });
    }
    layui.use(['form', ], function () {
        var form = layui.form
            , layer = layui.layer
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 2) {
                    return '标题至少得2个字符啊';
                }
            }
        });
        $("#cancelAdd").click(function(e){
            e.preventDefault();
            parent.CloseChild(false)
        })
        //监听提交
        form.on('submit(menu)', function (data) {

            subData(data.field)
            return false;
        });
        form.on('select(menuType)',function(data){
            console.log(data)
            if(data.value==1){
                form.val('menu', {
                    "parentId":-1,

                })
                $("select[name='parentId']").attr('disabled', 'disabled');
                form.render('select')

            }else{
                $("select[name='parentId']").removeAttr('disabled');

                form.render('select')
            }
        })
        form.on('select(parentId)',function(data){
            if(data.value==-1){
                form.val('menu', {
                    "menuType":1,
                })
                $("select[name='menuType']").attr('disabled', 'disabled');

                form.render('select')

            }else{
                $("select[name='menuType']").removeAttr('disabled');
                
                form.render('select')
            }
        })
        //表单初始赋值

        form.val('menu', {
            "menuId":{{.data.Id}}
            ,"title": "{{.data.Title}}"
            , "menuType": {{.data.MenuType}}
            , "parentId": {{.data.ParentId}}
            , "path": "{{.data.Path}}" //开关状态
            , "pathParam": "{{.data.PathParam}}"
            , "pathMethod": "{{.data.PathMethod}}"
            , "isShow": {{.data.IsShow}}
            , "status": {{.data.Status}}
            , "orderNum": {{.data.OrderNum}}
            , "bak": "{{.data.Bak}}"
        })



    });
</script>